/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __entity_behavior
#define __entity_behavior 1

#include <string>
#include <boost/unordered_map.hpp>

#include <Core/Entity/Property.h>

namespace galaxy {
	namespace entity {

		class Entity;

		/** This class models behavior of entities. It may contain boost::weak_ptr 
		* references to Property objects that are registered in the owner Entity.
		* Specialized classes need to implement clone(), initialize() and update().
		*/
		class Behavior
		{	
		public:
			typedef enum _status {
				UNKNOWN = 0,        /**< the state is undefined (not usable) */
				CREATED = 1,        /**< the behavior is created but not initialized yet (not usable) */
				INITIALIZING = 2,   /**< the behavior is being initialized (not usable) */
				ACTIVE = 3,         /**< the state is activily being updated (usable) */
				INACTIVE = 4,       /**< the state is ignored (but usable) */
				FACTORY = 5,        /**< \b this is a factory object used by BehaviorFactory. */
			} Status;

		protected:

			Entity      * owner;	/**< the owning entity. This is where the properties are located */
			Status        state;	/**< the behavior's state */

		public:
			Behavior(); /**< default constructor */
			virtual ~Behavior(void); /**< destructor */

			/** Clone \b this to produce a new behavior object (same class). The newly created 
			* behavior will not have an owner entity and its state will be \b CREATED.
			* It will call the virual method 'clone'.
			* @return a newly created property
			* @internal
			*/
			Behavior * cloneBehavior ();

			/** Initialize \b this with the required properties and attributes.
			* This method will call virtual method initialize(). Before calling the state
			* of the behavior is set to \b INITIALIZING.
			* @pre the object's owner entity must be set to a valid Entity object.
			* @post the state of the object is set to \b INACTIVE if the method succeeds 
			* and to \b CREATED when failed.
			*/
			bool initializeBehavior ();

			/** updates the entity with \b this behavior. The behavior may update properties that are
			* registered in the owning entity.
			* If the state of the behavior is \b ACTIVE, then update() is called.
			* @return returns the result of update() if the behavior is active or \b false otherwise
			*/
			bool updateBehavior ();

			/** get the owner entity */
			Entity * getOwner () { return owner; };
			/** set the owner entity */
			void setOwner (Entity& entity) { owner = &entity; };

			/** get the current state */
			Status getState () { return state; };
			/** set the current state */
			void setState (Status newstate) { state = newstate; };

			/** create a new behavior object. Since this is a pure virtual method, the specializing class must implement it.
			* For example:
			* \code
			* bool TstBehavior::clone() {
			*   return new TstBehavior();
			* }
			* \endcode
			* @return a new behavior object or NULL upon error
			*/
			virtual Behavior * clone () = 0;
			/** initialize the behavior */
			virtual bool initialize () = 0;
			/** update the behavior */
			virtual bool update () = 0;
		};

	}
}

#endif // __entity_behavior //

